/*
 * [Ronny Abraham]
 * [ronnya@gmail.com]
 * CS530:DUI, Assignment [#1]
 */

import javax.swing.JPanel;


/*
 * PreviewController 
 */

public class PreviewController
{
	public PreviewController()
	{
	}

	public JPanel createView(Actor actor)
	{
		itsPreviewView = new PreviewView(this, actor);
		return itsPreviewView.itsPreviewPanel;
	}
	
	public void setVisible(boolean isVisible)
	{
		itsPreviewView.itsPreviewPanel.visible = isVisible; 
		itsPreviewView.itsPreviewPanel.repaint();
	}
	
	/*
	 * geometryChanged
	 * change the preview when the geometry changes
	 */
	public void geometryChanged(int width, int height, Expression exp)
	{
		float factorX = (float)width/100;
		float factorY = (float)height/150;

		ActorGeometry newGeom = new ActorGeometry();
		setExpressionGeometry(newGeom, exp);
		scaleAroundCenter(newGeom, factorX, factorY);
		newGeom.worldToPanel();
		itsPreviewView.itsPreviewPanel.geom = newGeom;
		itsPreviewView.itsPreviewPanel.repaint();
	}
	
	private void setExpressionGeometry(ActorGeometry geom, Expression exp)
	{
		if(exp == Expression.neutral)
		{
			itsPreviewView.setArcVisible(false);
		}
		else
		{
			itsPreviewView.setArcVisible(true);
			if(exp == Expression.sad)
			{
				geom.mouthPos.x = 80;
				geom.mouthPos.y = 185;
				geom.startAngle = 45;
				geom.arcAngle = 90;
			}
		}
	}

	/*
	 * scaleAroundPoint
	 * Returns an actor which is the current actor scaled around the center of the actor
	 * To scale around a point be we first translate by -p, then scale and translate back by p.
	 */
	public void scaleAroundCenter(ActorGeometry actorGeom, float scaleX, float scaleY)
	{
		actorGeom.translate(-PreviewPanel.actorCenter.x, -PreviewPanel.actorCenter.y);
		actorGeom.scale(scaleX, scaleY);
		actorGeom.translate(PreviewPanel.actorCenter.x, PreviewPanel.actorCenter.y);
	}
	
	public void setShirtColor(ClothesColor color)
	{
		itsPreviewView.itsPreviewPanel.itsShirtColor = ClothesColor.getColorVal(color);
		itsPreviewView.itsPreviewPanel.repaint();
	}
	
	public void setPantsColor(ClothesColor color)
	{
		itsPreviewView.itsPreviewPanel.itsPantsColor = ClothesColor.getColorVal(color);
		itsPreviewView.itsPreviewPanel.repaint();
	}
	
	public void setWidth(int width)
	{
		//itsPreviewView.itsWidthVal.setText(new Integer(width).toString());
	}
	
	public void setHeight(int height)
	{
		//itsPreviewView.itsHeightVal.setText(new Integer(height).toString());
	}
	
	public PreviewView itsPreviewView;
}
